##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_new_invoice_line {} {
	set new_inv_l [top_win "Add new line to invoice"]
	set inv_line_frame [ttk::frame $new_inv_l.top]
	set ::daidze_niline [xml_wrapper new]
	pack $inv_line_frame
	$::daidze_niline load_main invoice add_new_invoice_line.xml $inv_line_frame
}

proc remove_new_invoice_line {table_var} {
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	$my_tablelist delete [$my_tablelist curselection]
	$::daidze_main update_form [dict create total [$::daidze_main sum_tablelist_column $table_var 11]]
}

proc invoice_type {} {
	set inv_types [$::db select_all daidze_doc_type name "code = \'invoice\' OR code = \'proforma\' OR code = \'invoice-receipt\'"]
	set inv_type_list [list]
	foreach tmp [dict values $inv_types] {
		lappend inv_type_list [::msgcat::mc [dict get $tmp name]]
	}
	return $inv_type_list
}

proc add_invoice_line {} {
	set values [$::daidze_niline get_values]
	set msg_list [dict create qty "Quantity field is obligatory." fk_product "Product field is obligatory."]
	set flags [list]
	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 
	
	if {[lsearch -not -exact $flags 1] == "-1"} {
		set line [dict create]
		set l_list [::invoice::calculate_invoice_line [dict values $values]]
		for {set x 0} {$x < [llength $l_list]} {incr x} {
			dict set line $x [lindex $l_list $x]
		}
		if {$::empty_stock_sale_enable eq "no"} {

			if {$::default_warehouse_id != 0} {
				set def_warehouse [lindex [$::db select_all warehouse "name" "id=$::default_warehouse_id" list] 0]
			} else {
				if {[lindex [$::daidze_main get_values list fk_warehouse] 0] ne ""} {
					set def_warehouse [$::daidze_main get_values list fk_warehouse]
				} else {
					set def_warehouse [lindex [$::db select_all warehouse "name" "def=1" list] 0]
				}
			}
			
			if {$def_warehouse ne ""} {
				set a [::stock::check_product_on_stock $def_warehouse [dict get $values fk_product]]
				if {$a >= [dict get $values qty]} {
					$::daidze_main update_form [dict create invoice_lines [dict create 0 $line] table_clear 0]
					$::daidze_main update_form [dict create total [$::daidze_main sum_tablelist_column "invoice_lines" 11]]
					destroy .topwin
				} else {
					tk_messageBox -icon info -type ok -message "[::msgcat::mc "Not enough quantity on stock."]"
				}
			} else {tk_messageBox -icon info -type ok -message "[::msgcat::mc "Define default warehouse or select one."]"}
		} else {
			$::daidze_main update_form [dict create invoice_lines [dict create 0 $line] table_clear 0]
			$::daidze_main update_form [dict create total [$::daidze_main sum_tablelist_column "invoice_lines" 11]]
			destroy .topwin
		}

	}
}

proc product_price {product_name {user_discount_name ""} {qty "1"} {user_tax_name ""}} {
	set result [list $qty $product_name]
	set work_dict [dict create qty $qty fk_product $product_name]

	if {$user_tax_name eq ""} {
		set query "SELECT product.default_price, product.default_price_with_tax, tax.name, tax.rate, tax.procent FROM product, tax WHERE product.fk_tax = tax.id AND product.name=\'$product_name\'"
	} else {
		set query "SELECT product.default_price, product.default_price_with_tax, tax.name, tax.rate, tax.procent FROM product, tax WHERE tax.name=\'$user_tax_name\' AND product.name=\'$product_name\'"
	}
	set prices_list [$::db execute_query $query]
	if {$user_discount_name eq ""} {
		set discount_list [list]
	} else {
		set query "SELECT discount.name, discount.value, discount.procent FROM discount WHERE discount.name=\'$user_discount_name\'"
		set discount_list [lindex [$::db execute_query $query] 0]
	}	

	dict append work_dict price [lindex [lindex $prices_list 0] 0]
	
# Calculate prices in case there is no discount at all
	if {[llength $discount_list] == 0} {
		dict append work_dict fk_tax [lindex [lindex $prices_list 0] 2]
		set applied_tax_list [::pos::value_plus_tax [dict get $work_dict price] [dict get $work_dict fk_tax]]
		dict append work_dict price_with_tax [lindex $applied_tax_list 0]
		dict append work_dict unit_tax [lindex $applied_tax_list 1]
		dict append work_dict subtotal_without_tax [round [expr double($qty) * double([dict get $work_dict price])]]
		dict append work_dict subtotal_with_tax [round [expr double($qty) * double([dict get $work_dict price_with_tax])]]
		dict append work_dict subtotal_tax [round [expr double([dict get $work_dict subtotal_with_tax]) - double([dict get $work_dict subtotal_without_tax])]]
		dict append work_dict discount_name ""
		dict append work_dict discount_value ""
		dict append work_dict derived_line_total [dict get $work_dict subtotal_with_tax]
	} else {
		if {$::discount_after_tax eq "yes"} {
			dict append work_dict fk_tax [lindex [lindex $prices_list 0] 2]
			set tax_value [lindex [lindex $prices_list 0] 3]
			set tax_procent [lindex [lindex $prices_list 0] 4]
		
			set applied_tax_list [::pos::value_plus_tax [dict get $work_dict price] [dict get $work_dict fk_tax]]
			dict append work_dict price_with_tax [lindex $applied_tax_list 0]
			dict append work_dict unit_tax [lindex $applied_tax_list 1]
			dict append work_dict subtotal_without_tax [round [expr double($qty) * double([dict get $work_dict price])]]
			dict append work_dict subtotal_with_tax [round [expr double($qty) * double([dict get $work_dict price_with_tax])]]
			dict append work_dict subtotal_tax [round [expr double([dict get $work_dict subtotal_with_tax]) - double([dict get $work_dict subtotal_without_tax])]]
			
			dict append work_dict discount_name [lindex $discount_list 0]
			
			if {[lindex $discount_list 2] eq "1" && [lindex $discount_list 1] > 0} {
				set discount_value [round [expr [dict get $work_dict subtotal_with_tax] - [::subtract_procent [dict get $work_dict subtotal_with_tax] [lindex $discount_list 1]]]]
				if {$discount_value >= [dict get $work_dict subtotal_with_tax]} {
					tk_messageBox -icon error -type ok -message "[::msgcat::mc "You can't apply such discount."]"
					dict set work_dict discount_name ""
					dict append work_dict discount_value ""
					dict append work_dict derived_line_total [dict get $work_dict subtotal_with_tax]
				} else {
					dict append work_dict discount_value $discount_value
					dict append work_dict derived_line_total [round [expr double([dict get $work_dict subtotal_with_tax]) - double([dict get $work_dict discount_value])]]
				}
			} elseif {[lindex $discount_list 1] > 0} {
				set discount_value [expr [dict get $work_dict subtotal_with_tax] - ([dict get $work_dict subtotal_with_tax] - [lindex $discount_list 1])]
				if {$discount_value >= [dict get $work_dict subtotal_with_tax]} {
					tk_messageBox -icon error -type ok -message "[::msgcat::mc "You can't apply such discount."]"
					dict set work_dict discount_name ""
					dict append work_dict discount_value ""
					dict append work_dict derived_line_total [dict get $work_dict subtotal_with_tax]
				} else {
					dict append work_dict discount_value $discount_value
					dict append work_dict derived_line_total [round [expr double([dict get $work_dict subtotal_with_tax]) - double([dict get $work_dict discount_value])]]
				}
			}
		}
		
	}
	set result [list [dict get $work_dict qty] [dict get $work_dict fk_product] [dict get $work_dict price] [dict get $work_dict price_with_tax] [dict get $work_dict fk_tax] [dict get $work_dict unit_tax] [dict get $work_dict subtotal_without_tax] [dict get $work_dict subtotal_with_tax] [dict get $work_dict subtotal_tax] [dict get $work_dict discount_name] [dict get $work_dict discount_value] [dict get $work_dict derived_line_total]]
}

proc calculate_invoice_line {line_val} {
	set inv_line [list]
	set qty [lindex $line_val 0]
	set product_name [lindex $line_val 1]
	set tax_name [lindex $line_val 2]
	set tmp_discount [lindex $line_val 3]
	set inv_line [::invoice::product_price $product_name $tmp_discount $qty $tax_name]

	return $inv_line
}


proc get_doc_type {translated_name} {
	set doc_types [$::db select_all daidze_doc_type]
	foreach line_num [dict keys $doc_types] {
		if {[::msgcat::mc "[dict get [dict get $doc_types $line_num] name]"] eq $translated_name} {
			set doc_type [list [dict get [dict get $doc_types $line_num] id] [dict get [dict get $doc_types $line_num] name] [dict get [dict get $doc_types $line_num] code]]
		}
	}
	return $doc_type
}

proc check_type {w} {
	set translated_name [$w get]
	if {$translated_name ne ""} {
		set doc_type [get_doc_type $translated_name]
		if {[lindex $doc_type 2] eq "invoice-receipt"} {
			$::daidze_main state_widget {fk_payment_type} readonly
		} else {
			$::daidze_main state_widget {fk_payment_type} disabled 1
		}
	} else {
		$::daidze_main state_widget {fk_payment_type} disabled 1
	}
}

proc add_invoice {} {
	[dict get [$::daidze_main get_fdata] invoice_lines] selection set 0 end
	set values [$::daidze_main get_values]

	set msg_list [dict create fk_invoice_type "Invoice type field is obligatory." fk_client "Client name is obligatory." inv_date "Date field is obligatory." invoice_lines "You need to add at least one line to invoice."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		set doc_type [lindex [::invoice::get_doc_type [dict get $values fk_invoice_type]] 2]
		if {$doc_type eq "invoice" || $doc_type eq "invoice-receipt" } {
			$::db transaction start
			
			set inv_num [expr [$::db select_last_inserted invoice inv_num]+1]
			set custom_number "$inv_num/[now_date year]"
			set inv_time [time]
			# If there is no default warehouse selected trying to get default one or specific for this POS
			if {$::default_warehouse_id != 0} {
				set warehouse [lindex [$::db select_all warehouse "name" "id=$::default_warehouse_id" list] 0]
				puts  "$warehouse"
			} else {
				if {[dict get $values fk_warehouse] ne ""} {
					set warehouse [dict get $values fk_warehouse]
				} else {
					set warehouse [lindex [$::db select_all warehouse "name" "def=1" list] 0]
					if {$warehouse eq ""} {
						tk_messageBox -icon error -type ok -message "[::msgcat::mc "We were unable to save your invoice because you don't have default warehouse define."]"
					} else {
						set warehouse $warehouse
					}
				}
			}

			set data_inv_tbl [dict create inv_num $inv_num custom_number $custom_number inv_time $inv_time fk_daidze_user $daidze_users::name fk_warehouse $warehouse]
			set data_inv_l_tbl [dict create]
			set inv_col [list fk_client inv_date total]
			
			foreach key $inv_col {
				dict append data_inv_tbl $key [dict get $values $key]
			}
			$::db insert_all invoice $data_inv_tbl

			set inv_l_col [list fk_invoice qty fk_product]

			set stock_tmp [dict create]
			set query "SELECT id FROM stock_movement_type WHERE sale_mov = \'1\'"
			dict append stock_tmp stock_movement_type [$::db execute_query $query]
			

			dict append stock_tmp fk_warehouse $warehouse

			
			dict append stock_tmp mov_date [dict get $values inv_date]
			dict append stock_tmp mov_time $inv_time
			dict append stock_tmp inv_num $inv_num
			
			set invoice_lines [dict get $values invoice_lines] 
			foreach j [dict keys $invoice_lines] g [dict values $invoice_lines] {
				dict lappend g invoice $inv_num
				dict set g discount [$::db select_id_by_name discount [dict get $g discount]]
				dict set g price [dict get $g subtotal_without_tax]
				set g [dict remove $g price_with_tax unit_tax subtotal_without_tax subtotal_with_tax]

				$::db insert_all invoice_lines $g

				# Stock movement
				if {$::stock_enable eq "yes"} {
					dict set stock_tmp qty [dict get $g qty]
					dict set stock_tmp fk_product [dict get $g fk_product]
					$::db insert_all stock_current $stock_tmp
				}
			}
			
			if {$doc_type eq "invoice-receipt"} {
				set pay_data [dict create fk_client [dict get $data_inv_tbl fk_client] pay_date [dict get $data_inv_tbl inv_date] pay_time [dict get $data_inv_tbl inv_time] amount [dict get $data_inv_tbl total] invoice [dict get $data_inv_tbl inv_num]]
			
				if {[dict get $values fk_payment_type] eq ""} {
					# Looking for local payment type from config db
					if {$::default_payment_id != 0} {
						dict append pay_data fk_payment_type [lindex [$::db select_all payment_type "name" "id=$::default_payment_id" list] 0]
					} else {
						set query "SELECT name FROM payment_type WHERE def = \'1\'"
						set pay_type [lindex [lindex [$::db execute_query $query] 0] 0]
						if {$pay_type eq ""} {
							tk_messageBox -icon error -type ok -message "[::msgcat::mc "We are unable to save your invoice because you dont have default payment type defined."]"
						} else {
							dict set pay_data fk_payment_type $pay_type
						}
					}
				} else {
					dict set pay_data fk_payment_type [dict get $values fk_payment_type]
				}

				$::db insert_all payment $pay_data
			}
			$::db transaction stop
			$::daidze_main load_main invoice invoice.xml $::main 1

		} elseif {$doc_type eq "proforma"} {
			$::db transaction start
			set pro_num [expr [$::db select_last_inserted proforma pro_num]+1]
			set custom_number "$pro_num/[now_date year]"
			set pro_time [time]

			set data_pro_tbl [dict create pro_num $pro_num custom_number $custom_number pro_time $pro_time fk_daidze_user $daidze_users::name]
			set data_pro_l_tbl [dict create]
			set pro_col [list fk_client total]
			
			foreach key $pro_col {
				dict append data_pro_tbl $key [dict get $values $key]
			}
			dict append data_pro_tbl pro_date [dict get $values inv_date]
			
			$::db insert_all proforma $data_pro_tbl

			set pro_l_col [list fk_invoice qty fk_product]

			set proforma_lines [dict get $values invoice_lines] 
			foreach j [dict keys $proforma_lines] g [dict values $proforma_lines] {
				dict lappend g proforma $pro_num
				dict set g discount [$::db select_id_by_name discount [dict get $g discount]]
				dict set g price [dict get $g subtotal_without_tax]
				set g [dict remove $g price_with_tax unit_tax subtotal_without_tax subtotal_with_tax]
				$::db insert_all proforma_lines $g
			}
			$::db transaction stop
			$::daidze_main load_main invoice proforma.xml $::main 1
		} 
	}
}

proc edit_invoice_form {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]

	set vars {inv_num custom_number fk_client inv_date inv_time warehouse total}
	
	if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]

		foreach key $vars value $row {
			dict set sdata $key $value
		}
		
		set inv_id [dict get $sdata inv_num]
		set result [$::db select_all invoice_lines {qty fk_product price fk_tax discount derived_tax derived_discount derived_line_total} "invoice = \'$inv_id\'"]

		
		# This loop changing discount id to discount name, and recalculate missing data for invoice lines
		set final_result [dict create]
		set inv_line_keys [list qty fk_product price price_with_tax fk_tax unit_tax subtotal_without_tax subtotal_with_tax derived_tax discount derived_discount derived_line_total ]
		foreach tmp_key [dict keys $result] tmp_val [dict values $result] {
			set discount_name [lindex [$::db select_name_by_id discount [dict get $tmp_val discount]] 0]
			set a [::invoice::calculate_invoice_line [list [dict get $tmp_val qty] [dict get $tmp_val product_name] [dict get $tmp_val tax_name] $discount_name]]
			set line [dict create]
			foreach val $a key $inv_line_keys {
				dict append line $key $val
			}
			dict append final_result $tmp_key $line
		}
		
		# Search stock_table for inv_num and discover warehouse name
		set warehouse [lindex [$::db select_all stock_current "fk_warehouse" "inv_num=$inv_id" list] 0]
		dict append sdata fk_warehouse $warehouse

		$::daidze_main load_main invoice edit_invoice.xml $::main
		dict append sdata invoice_lines $final_result
		$::daidze_main update_form $sdata
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry to edit."]"
	}
}

proc edit_proforma_form {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]

	set vars {pro_num custom_number fk_client pro_date pro_time total}

	if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]

		foreach key $vars value $row {
			dict set sdata $key $value
		}
		
		set proforma_id [dict get $sdata pro_num]
		set result [$::db select_all proforma_lines {qty fk_product price fk_tax discount derived_tax derived_discount derived_line_total} "proforma = \'$proforma_id\'"]

		
		# This loop changing discount id to discount name, and recalculate missing data for invoice lines
		set final_result [dict create]
		set inv_line_keys [list qty fk_product price price_with_tax fk_tax unit_tax subtotal_without_tax subtotal_with_tax derived_tax discount derived_discount derived_line_total ]
		foreach tmp_key [dict keys $result] tmp_val [dict values $result] {
			set discount_name [lindex [$::db select_name_by_id discount [dict get $tmp_val discount]] 0]
			set a [::invoice::calculate_invoice_line [list [dict get $tmp_val qty] [dict get $tmp_val product_name] [dict get $tmp_val tax_name] $discount_name]]
			set line [dict create]
			foreach val $a key $inv_line_keys {
				dict append line $key $val
			}
			dict append final_result $tmp_key $line
		}
		
		$::daidze_main load_main invoice edit_proforma.xml $::main
		dict append sdata invoice_lines $final_result
		$::daidze_main update_form $sdata
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry to edit."]"
	}
}

proc edit_invoice {} {
	[dict get [$::daidze_main get_fdata] invoice_lines] selection set 0 end
	set values [$::daidze_main get_values]

	set msg_list [dict create fk_client "Client name is obligatory." inv_date "Date field is obligatory." invoice_lines "You need to add at least one line to invoice."]
	set flags [list]
	
	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	}
	
	if {[lsearch -not -exact $flags 1] == "-1"} {
		$::db transaction start
		set inv_time [time]
		set inv_col [list inv_num custom_number fk_client inv_date total]
		foreach key $inv_col {
			dict append data_inv_tbl $key [dict get $values $key]
		}
		
		set invoice_num [dict get $data_inv_tbl inv_num]
		
		$::db update_all invoice $data_inv_tbl
		$::db delete_rows_in_relative invoice_lines invoice $invoice_num
		$::db delete_rows_in_relative stock_current inv_num $invoice_num

		# Update payments
		set payment_data [dict create]
		dict append payment_data invoice [dict get $data_inv_tbl inv_num]
		dict append payment_data fk_client [dict get $data_inv_tbl fk_client]
# 		dict append payment_data pay_date [dict get $data_inv_tbl inv_date]
# 		dict append payment_data amount [dict get $data_inv_tbl total]

		$::db update_all payment $payment_data
		
		set inv_l_col [list fk_invoice qty fk_product]

			set stock_tmp [dict create]
			set query "SELECT id FROM stock_movement_type WHERE sale_mov = \'1\'"
			dict append stock_tmp stock_movement_type [$::db execute_query $query]

			# If there is no default warehouse selected trying to get default one or specific for this POS
			if {$::default_warehouse_id != 0} {
				dict append stock_tmp fk_warehouse [lindex [$::db select_all warehouse "name" "id=$::default_warehouse_id" list] 0]
			} else {
				if {[dict get $values fk_warehouse] ne ""} {
					dict append stock_tmp fk_warehouse [dict get $values fk_warehouse]
				} else {
					set warehouse [lindex [$::db select_all warehouse "name" "def=1" list] 0]
					if {$warehouse eq ""} {
						tk_messageBox -icon error -type ok -message "[::msgcat::mc "We are unable to save your invoice because you dont have warehouse define."]"
					} else {
						dict append stock_tmp fk_warehouse $warehouse
					}
				}
			}
			
			dict append stock_tmp mov_date [dict get $values inv_date]
			dict append stock_tmp mov_time $inv_time
			dict append stock_tmp inv_num $invoice_num
			
			set invoice_lines [dict get $values invoice_lines] 
			foreach j [dict keys $invoice_lines] g [dict values $invoice_lines] {
				dict lappend g invoice $invoice_num
				dict set g discount [$::db select_id_by_name discount [dict get $g discount]]
				dict set g price [dict get $g subtotal_without_tax]
				set g [dict remove $g price_with_tax unit_tax subtotal_without_tax subtotal_with_tax]

				$::db insert_all invoice_lines $g

				# Stock movement
				if {$::stock_enable eq "yes"} {
					dict set stock_tmp qty [dict get $g qty]
					dict set stock_tmp fk_product [dict get $g fk_product]
					$::db insert_all stock_current $stock_tmp
				}
			}
		$::db transaction stop
		$::daidze_main load_main invoice invoice.xml $::main 1
	}
}

proc edit_proforma {} {
	[dict get [$::daidze_main get_fdata] invoice_lines] selection set 0 end
	set values [$::daidze_main get_values]

	set msg_list [dict create fk_client "Client name is obligatory." pro_date "Date field is obligatory." invoice_lines "You need to add at least one line to invoice."]
	set flags [list]
	
	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	}
	
	if {[lsearch -not -exact $flags 1] == "-1"} {
		$::db transaction start
		set pro_time [time]
		set pro_col [list pro_num custom_number fk_client pro_date total]
		foreach key $pro_col {
			dict append data_inv_tbl $key [dict get $values $key]
		}
		
		set pro_num [dict get $data_inv_tbl pro_num]
		
		$::db update_all proforma $data_inv_tbl
		$::db delete_rows_in_relative proforma_lines proforma $pro_num

		set invoice_lines [dict get $values invoice_lines] 
		foreach j [dict keys $invoice_lines] g [dict values $invoice_lines] {
			dict lappend g proforma $pro_num
			dict set g discount [$::db select_id_by_name discount [dict get $g discount]]
			dict set g price [dict get $g subtotal_without_tax]
			set g [dict remove $g price_with_tax unit_tax subtotal_without_tax subtotal_with_tax]

			$::db insert_all proforma_lines $g
		}
		$::db transaction stop
		$::daidze_main load_main invoice proforma.xml $::main 1
	}
}

proc fast_payment {} {
	set values [$::daidze_main get_values]
	if {[dict get $values invoice] == ""} {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select invoice, please!"]"
	} else {

		foreach val [dict get $values invoice] {
			set line $val 
		}
		if {[dict get $line debt] > 0} {
			set new_data [dict create fk_client [dict get $line client_name] pay_date [date] amount [dict get $line debt] invoice [dict get $line invoice_id]]
			$::daidze_main load_main payment add_fast_payment.xml $::main
			$::daidze_main update_form $new_data
		} else {
			tk_messageBox -icon info -type ok -message "[::msgcat::mc "It seems to me, that client has no debt with us."]"
		}
	}
}


proc print_invoice {} {
	if {[set values [dict get [$::daidze_main get_values] invoice]] != ""} {
		set print_data [dict get $values 0]

		set id [$::db select_id_by_name client [dict get $print_data client_name]]
		set cli_vals [$::db select_all client {street phone mobile email web fin} "id=\'$id\'" list]
		foreach col {street phone mobile email web fin} cli_val $cli_vals {
			dict append print_data $col $cli_val
		}
		
		set inv_lines [$::db select_all invoice_lines "qty fk_product price derived_tax derived_discount derived_line_total" "invoice=\'[dict get $print_data invoice_id]\'"]
		set inv_aggr_dict [$::db select_all aggregated_invoice_lines * "invoice=\'[dict get $print_data invoice_id]\'"]
		set inv_aggr_data [dict get $inv_aggr_dict 0]
		dict append print_data inv_lines $inv_lines
		foreach key [dict keys $inv_aggr_data] val [dict values $inv_aggr_data] {
			dict append print_data $key $val
		}
		
		set company_data [$::db select_all company {name slogan slogan2 tel tel2 tel3 fin address address2 address3 fax fax2 fax3 email email2 email3 mobile mobile2 mobile3 web web2 web3 logo logo2 logo3 description} {def='1'} list]
		foreach company_key {company_name company_slogan company_slogan2 company_tel company_tel2 company_tel3 company_fin company_address company_address2 company_address3 company_fax company_fax2 company_fax3 company_email company_email2 company_email3 company_mobile company_mobile2 company_mobile3 company_web company_web2 company_web3 logo logo2 logo3 company_description} val $company_data {
			dict append print_data $company_key $val
		}
		dict append print_data general $inv_aggr_dict
		
		set pdf [my_print new [file join $::models invoice_general.xml] $print_data]
		$pdf destroy 
	}
}

proc print_proforma {} {
	if {[set values [dict get [$::daidze_main get_values] proforma]] != ""} {
		set print_data [dict get $values 0]

		set id [$::db select_id_by_name client [dict get $print_data fk_client]]
		set cli_vals [$::db select_all client {street phone mobile email web fin} "id=\'$id\'" list]
		foreach col {street phone mobile email web fin} cli_val $cli_vals {
			dict append print_data $col $cli_val
		}
		
		set inv_lines [$::db select_all proforma_lines "qty fk_product price derived_tax derived_discount derived_line_total" "proforma=\'[dict get $print_data pro_num]\'"]
		set inv_aggr_dict [$::db select_all aggregated_proforma_lines * "proforma=\'[dict get $print_data pro_num]\'"]
		set inv_aggr_data [dict get $inv_aggr_dict 0]
		dict append print_data inv_lines $inv_lines
		foreach key [dict keys $inv_aggr_data] val [dict values $inv_aggr_data] {
			dict append print_data $key $val
		}
		
		set company_data [$::db select_all company {name slogan slogan2 tel tel2 tel3 fin address address2 address3 fax fax2 fax3 email email2 email3 mobile mobile2 mobile3 web web2 web3 logo logo2 logo3 description} {def='1'} list]
		foreach company_key {company_name company_slogan company_slogan2 company_tel company_tel2 company_tel3 company_fin company_address company_address2 company_address3 company_fax company_fax2 company_fax3 company_email company_email2 company_email3 company_mobile company_mobile2 company_mobile3 company_web company_web2 company_web3 logo logo2 logo3 company_description} val $company_data {
			dict append print_data $company_key $val
		}
		dict append print_data general $inv_aggr_dict

		set pdf [my_print new [file join $::models proforma_general.xml] $print_data]
		$pdf destroy 
	}
}